/* 
 * JUGJobs -- A Jobs Posting Application for Java Users Groups and Other Groups
 * $Id: JobList.java,v 1.3 2006/06/26 05:02:31 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JUGJobs project.
 *
 * The Initial Developers of the Original Code are the members of the Triangle
 * Java User's Group in the RTP area of North Carolina.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developers. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.jugjobs.model;

import org.jugjobs.controller.Logger;
import org.jugjobs.exception.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;

import org.jugjobs.form.JobBean;


/**
 * @author BigLee
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JobList {

	  private static Logger logger = new Logger();
	  private static ArrayList postingsList = new ArrayList();
	  private static boolean postingsListCurrent;
	  private static Database database;
	  
	  public JobList() {}
	  public JobList(HttpServlet servlet) {
		  postingsListCurrent=false;
		  database = new Database(servlet);		  
	  }
	  
	  public boolean passPassword(int jobNumber, String submittedPassword)
	        throws SQLException{
	   String realPassword = database.getPosting(jobNumber).getPosting_password();
	   return realPassword.equals(submittedPassword);
	  }
	  
	  public ArrayList getPostingsList() throws DatabaseException
	  {
	    if(!postingsListCurrent){
	      logger.log("getting new posting list");
	      synchronized(postingsList){
	        try {
				postingsList = database.getAllPostings();
				postingsListCurrent = true;
			} 
	        catch (SQLException e) 
			{
	        	logger.log("SQL Exception",e);
	        	throw new DatabaseException ("Can't get jobs from database.", e);
			}
	      }
	    }
	    logger.log("returning postingsList with "+postingsList.size()+" entries.");
	    return postingsList;
	  }
	  
	  public synchronized JobBean getPosting(int jobNumber) 
	              throws SQLException{
	    return database.getPosting(jobNumber);
	  }
	  
	  public synchronized void insertPosting(JobBean posting) 
	              throws SQLException{
	    Connection connection = database.connectToDb();
	    try{
	      postingsListCurrent = false;
	      database.insertPosting(connection, posting);
	    }finally{
	      connection.close();
	    }
	  }
	  
	  public synchronized void deletePosting(int jobNumber) throws SQLException{
	    Connection connection = database.connectToDb();
	    try{
	      postingsListCurrent = false;
	      database.deletePosting(connection, jobNumber);
	    }finally{
	      connection.close();
	    }
	  }

	  public synchronized void replacePosting(JobBean posting)
	              throws SQLException{
	    Connection connection = database.connectToDb();
	    try{
	      postingsListCurrent = false;
	      int jobNumber = Integer.parseInt(posting.getJob_number());
	      database.deletePosting(connection, jobNumber);
	      database.insertPosting(connection, posting);
	    }finally{
	      connection.close();
	    }
	  }
	}